/**
 * https://ac.nowcoder.com/acm/problem/233601
 */

#include<iostream>
#include<queue>
using namespace std;
typedef long long ll;
int main()
{
    ll n;
    cin>>n;
    priority_queue<ll,vector<ll>,greater<ll>> p;
    while(n--){
        ll k;
        cin>>k;
        p.push(k);
    }
    ll sum=0;
    while(p.size()>1){
        ll a=p.top();
        p.pop();
        ll b=p.top();
        p.pop();
        sum+=(a+b);
        p.push(a+b);
    }
    cout<<sum<<endl;
    return 0;
}